<?php
$libdir = dirname(__FILE__);
require_once "{$libdir}/coreLib.php";
require_once "{$libdir}/htmlTag.php";
class coreHtml extends lib{
	/**
	 * Basic implementation of a HTML site
	 */
	private $title;
	private $head_tags = array();  
	private $body_tags = array();
	private $css = array();
	public function __construct($version = 'html5'){
		switch($version){
			case 'html5':
			default:
		}	
	}
	/**
	* Load and return basic template for HTML 5
	*/
	public function load_html5(){
		
		$return = "";
		$return.="<!DOCTYPE html>\n";
		$return.="<html>\n";
		$return.="<head>\n";
		$return.="<style><!--\n";
		foreach($this->css as $rule){
			$return.=$rule->load();
		}
		$return.="\n--> </style>\n";
		$return.=$this->load_head_tags()."\n";
		$return.=$this->title->toHtml();
		$return.="</head>\n";
		$return.="<body>\n";
		$return.=$this->load_body_tags()."\n";
		$return.="</body>\n";
		$return.="</html>\n";
		return $return;
	}
	
	/*
	*	Adds a new CSS rule to the page
	*	@param CssRule $rule the CSS object for the rule
	*/
	public function addCss(CssRule $rule){
		$this->css[]=$rule;
	}
	
	/*
	*	Load and fetch the HTML code intended for the <Head> section
	*/
	protected function load_head_tags(){
		$head = "";
		foreach($this->head_tags as $tag){
			$head.=$tag->toHtml(null,true);
		}
		return $head;
	}
	
	/*
	*	Load and fetch the HTML code intended for the <BODY> section
	*/
	protected function load_body_tags(){
		$body = "";
		foreach($this->body_tags as $tag){
			$body.=$tag->toHtml(null,false);
		}
		return $body;
	}
	
	/**
	* Register a new tag for the <HEAD> section
	* @param htmlTag $tag The tag object to be added
	*/
	public function addHeadTag(htmlTag $tag){
		if(!$tag instanceof htmlTag){
			echo 'Invalid Tag:<pre>';
			var_dump($tag);
			return;
		}
		$this->head_tags[]= $tag;
	}
	/**
	 * 
	 * Add a preformated tag to the body
	 * @param htmlTag $tag The tag object to be added
	 */
	public function addBodyTag(htmlTag $tag){
		if(!$tag instanceof htmlTag){
			echo 'Invalid Tag:<pre>';
			var_dump($tag);
			return;
		}
		$this->body_tags[]= $tag;
	}
	
	
	public function setTitle($title){
		if(!$this->title instanceof htmlTag){
			$this->title = new htmlTag('title',$title);
			$this->title->closeTag = true;
			$this->title->isHead = true;
		}
		$this->title->setContent($title);
	}
	public function deleteHeadTag($tagid){
		
	}
	
}